actionCmpDistrNew fh name =  do
        doc <- findOne (select ["name" =: name] colName) -- get id of given directory
        if (doc == Nothing)
            then do liftIO $ print "bad name, dir not found"
            else do dir <- findOne (select ["docID"=:(id (fromJust doc))] "Distr") -- get distrinfo for given doc
		    liftIO $ print (show (dir))
                    if (isJust dir) -- did we find the directory? good
                     then do distrFiles <- lookupDistrFiles (fromJust dir) -- get ext info for given dir
                             dirs <- rest =<< find (select [] "Distr") -- retrieve all the other directories
                             result <- cmpDistr' (docToTuple (fromJust distrFiles)) dirs -- now compare given dir to all dirs
                             print'' fh (filter (\e -> (snd (snd e) /= 0)) result)
                     else liftIO $ hPutStrLn fh ("invalid dir id")
    where
        id' (ObjId v) = v
        id doc = id' (valueAt "_id" doc)
        lookupDistrFiles doc =
            do idOfFileDistr <- getFileId doc
               distrFiles <- findOne (select ["_id"=:idOfFileDistr] "DistrFiles") {project = ["_id"=:(0::Int)]}
               return distrFiles
        unwrap (ObjId v) = v
        getFileId dir = return $ unwrap (valueAt "distrFiles" dir)
        cmpDistr' dir1 docs = foldM (cmpDistr'' dir1) [] docs -- loop comparing given distr to each other dirs distr
        cmpDistr'' dir1 acc doc = do -- dir = [(string, integer)], comparing to document, need to convert
               distrFiles <- lookupDistrFiles doc
               let result = cmpDistrNew dir1 (docToTuple (fromJust distrFiles)) --convert here..
                    in return ((doc, result):acc)
                       
cmpDistrNew :: [(String, Integer)] -> [(String, Integer)] -> Double
cmpDistrNew distr1 distr2 = sizeInt/sizeUn * 100.0
    where
        getSet list = nubBy distrEq (converToDistr list)
        distr1' = getSet distr1
        distr2' = getSet distr2
        jacInt = intersectBy distrEq distr1' distr2'
        jacUn = unionBy distrEq distr1' distr2'
        sizeInt = fromIntegral (length jacInt)
        sizeUn = fromIntegral (length jacUn)


distrEq :: (String, Double) -> (String, Double) -> Bool
distrEq (ext1, distr1) (ext2, distr2) = (ext1 == ext2) && (distr2 >= d1Min) && (distr2 <= d1Max) 
      where
          d1Min = distr1 - 5.0
          d1Max = distr1 + 5.0

getTotal :: [(String, Integer)] -> Integer
getTotal xs = sum (snd (unzip xs))

convertToDistr :: [(String, Integer)] -> [(String, Double)]
convertToDistr list = map (helper (getTotal list)) list
     where
         helper :: Integer -> (String,Integer) -> (String, Double)
         helper t (ext, occurences) = (ext, dist)
              where
                dist = (fromIntegral occurrences)/(fromIntegral t)
                
                
                
                
                
                
                
--findAllDirectory = rest =<< find (select ["fileType"=:u"d/d"] colName)
--, make sure directory is valid, define comparedir method
correlatedDirsNew name fh = do
    valid <- findDirectory' name -- could also try findOne
    if valid == Nothing
        then return ()
        else do
          all <- (rest =<< find (select ["fileType" =: u"d/d"] colName))
          subfiles <- findSubDirs' (nameOfDir (fromJust valid))
          eDistance <- editDistanceCorr subfiles all
          print'' fh eDistance
        where
            nameOfDir x = name' (valueAt "name" x)
            name' (String v) =  C.unpack v

-- [Documents] -> CorrValueForEditDistance..
--- takes subdirectories of interesting directory, and list of directories to compare to.
--- and returns list of tuples, (Document, numHits)
-- take this set of documents and compare it to each directory's subdirs
editDistanceCorrNew subdirs dirs =  do result <- mapM (editDir2Dir subdirs) dirs
                                    return (filter (\e -> (snd e) /= 0)) result)

        --may change this, may return id, name, value, instead...
editDir2DirNew subdirs dir = do subfiles <- findSubDirs' (name' (valueAt "name" dir))
                             return (dir, jacIndex)
                          where 
                            jacIndex = jacName (getNames subdirs) (getNames subfiles)
                                  

getNames :: [Document] -> [String]
getNames docs = map getName docs 

--takes a document and returns a string with name and no path info
getName :: Document -> String
getName doc = getLast (split "/" (name doc))
     where
         name doc = name' (valueAt "name" doc)
         name' (String v) = C.unpack v
         getLast (x:[]) = x
         getLast (x:xs) = getLast xs
        
        
--two lists of strings with names from each docs
--assumes that I have only filenames.. with no path...
jacName :: [Strings] -> [Strings] -> Double
jacName dir1 dir2 = sizeInt/sizeUn * 100.0
     where  
        getSet list = nubBy nameEq list
        dir1' = getSet dir1
        dir2' = getSet dir2
        jacInt = intersectBy nameEq dir1' dir2'
        jacUn = unionBy nameEq dir1' dir2'
        sizeInt = fromIntegral (length jacInt)
        sizeUn = fromIntegral (length jacUn)
         

nameEq :: String -> String -> Bool
nameEq name1 name2 = (levenshteinDistance defaultEditCosts name1 name2) <= 5
        
calcTimelineRangeNew name fh = do
    valid <- findDirectory' name -- could also try findOne
    if valid == Nothing
        then do liftIO $ hPutStrLn fh "bad directory name"
        else do
          -- get all directories except for the one already given
          all <- (rest =<< find (select ["fileType" =: u"d/d", "name" =: ["$ne"=:name]] colName))
          subfiles <- findSubDirs' (nameOfDir (fromJust valid))
          stampHits <- timestampCorrNew valid subfiles all
          print'' fh stampHits
        where
            nameOfDir x = name' (valueAt "name" x)
            name' (String v) =  C.unpack v
            

getTimeStampsList :: [Document] -> [Integer]
getTimeStampsList list = foldr getTimeStamps [] list

getTimeStamps :: Document -> [Integer] -> Integer
getTimeStamps doc acc = atime:mtime:ctime:crtime:acc
     where
        atime = time (valueAt "atime" doc)
        mtime = time (valueAt "mtime" doc)
        ctime = time (valueAt "ctime" doc)
        crtime = time (valueAt "crtime" doc)
        time (Int32 v) = Prelude.read (show v) :: Integer
        
timeSub2Sub subfiles1 subfiles2 = sizeInt/sizeUn * 100.0
     where
         getSet tsList = nubBy tsEq (getTimeStamps tsList)
         ts1 = getSet subfiles1
         ts2 = getSet subfiles2
         jacInt = intersectBy tsEq ts1 ts2
         jacUn = unionBy tsEq ts1 ts2
         sizeInt = fromIntegral (length jacInt)
         sizeUn = fromIntegral (length jacUn)
         
         
tsEq ts1 ts2 = (ts2 <= tsMax) && (ts2 >= tsMin)
  where
      tsMin = lower ts1
      tsMax = upper ts1
      lower t = t - daySeconds
      upper t = t + daySeconds

timestampCorrNew dir subfiles all = do result <- mapM (timeDir2DirNew subfiles) all
                                    return (filter (\e -> (snd e /= 0)) result)
            
timeDir2DirNew subfiles doc = do subfiles2 <- findSubDirs' (nameOfDir doc) 
                              return (doc, (timeSub2Sub subfiles subfiles2))
     where
         nameOfDir x = name' (valueAt "name" x)
         name' (String v) =  C.unpack v